#ifndef _024B5C93_314E_4371_BA9D_ACA94D717737_
#define _024B5C93_314E_4371_BA9D_ACA94D717737_

#include "event.hpp"
using qserv::event;
namespace testqserv 
{
    class server_work
    {
    public:
        typedef qserv::NetConnection<server_work> server_handler_type;

        server_work(LoggerPtr logger)
            :buffer_(2048),
            logger_(logger)
        {
            LOG4CXX_DEBUG(logger_, "[Serv] Start sync thread handle client");
        }

        void on_clear(server_handler_type& handler)
        {
        }

        void on_open(server_handler_type& handler)
        {
            unsigned short localport = handler.socket().local_endpoint().port();
            unsigned short peerport = handler.socket().remote_endpoint().port();
            string localaddr(handler.socket().local_endpoint().address().to_string());
            string peeraddr(handler.socket().remote_endpoint().address().to_string());
            LOG4CXX_INFO(logger_, boost::format("[Serv] peer[%1%:%2%] <---> qserv[%3%:%4%] created") % peeraddr % peerport % localaddr % localport);
            LOG4CXX_INFO(logger_, boost::format("[Serv] id=[%d]") % tlgettid());
            handler.async_read_some(boost::asio::buffer(buffer_));
        }

        void on_read(server_handler_type& handler, std::size_t bytes_transferred)
        {
            boost::shared_ptr<event> evtptr(new event(event::user, bytes_transferred, (char *)&buffer_[0]));
            handler.async_write(evtptr);
        }

        void on_write(server_handler_type& handler, std::size_t bytes_transferred)
        {
            handler.async_read_some(boost::asio::buffer(buffer_));
        }

        void on_close(server_handler_type& handler, const boost::system::error_code& e)
        {
            switch (e.value())
            {
                // Operation successfully completed.
            case 0:
            case boost::asio::error::eof:
                LOG4CXX_INFO(logger_, boost::format("[Serv] Peer close connection : %1%")% e.message());
                break;

                // Connection breaked.
            case boost::asio::error::connection_aborted:
            case boost::asio::error::connection_reset:
            case boost::asio::error::connection_refused:
                LOG4CXX_INFO(logger_, boost::format("[Serv] Peer connection exception: %1%")% e.message());
                break;

                // Other error.
            case boost::asio::error::timed_out:
            case boost::asio::error::no_buffer_space:
            default:
                LOG4CXX_INFO(logger_, boost::format("[Serv] ERROR : %1%") % e.message());
                break;
            }
        }

        void on_parent(server_handler_type& handler, boost::shared_ptr<event> event)
        {
        }

        void on_child(server_handler_type& handler, boost::shared_ptr<event> event)
        {
        }

    private:
        ByteArray buffer_;
        LoggerPtr logger_;
    };

} // namespace szsvss2

#endif // _024B5C93_314E_4371_BA9D_ACA94D717737_
